Checking string using regexp

I want to check a string if it has a format like an object number (e.g. 1.12.3) in preparation for a filter.

11: string strToken = "^0-9*(\.0-9*)*$"

75: string anfang = get(text1DBE)
76: Regexp reg = regexp strToken
77: if (reg anfang)
78: {
79: <do something>
80: }

here DOORS says:
-E- DXL: <Line:11> badly formed token ("0-9*(\.0-9)")
-E- DXL: <Line:11> syntax error
-E- DXL: <Line:76> incorrect arguments for function (regexp)
-E- DXL: <Line:76> incorrectly concatenated tokens
Checking the regular expression with Regex Coach or with the script of https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14464336&#14464336 I am getting no problem.

So what I am doing wrong?

Could anyone please write an example where a string variable is checked with the regular expression above?

I am using DOORS 9.2.0
StefanLieser - Wed May 19 10:16:16 EDT 2010

Re: Checking string using regexp
StefanLieser - Wed May 19 10:20:38 EDT 2010

Some display-problems above with the regular expression:

string strToken = "^[0-9]*(\.[0-9]*)*$"

-E- DXL: <Line:11> badly formed token ("[0-9]*(\.[0-9]*)*")

Re: Checking string using regexp
llandale - Wed May 19 12:09:25 EDT 2010

To get one back-slash, you need to back-slash the backslash:

string strToken = "^0-9*(\\.0-9*)*$"
print strToken

prints this:
^0-9*(\.0-9*)*$

  • Louie

Re: Checking string using regexp
Mathias Mamsch - Wed May 19 12:14:25 EDT 2010

StefanLieser - Wed May 19 10:20:38 EDT 2010
Some display-problems above with the regular expression:

string strToken = "^[0-9]*(\.[0-9]*)*$"

-E- DXL: <Line:11> badly formed token ("[0-9]*(\.[0-9]*)*")

You need to use double backslashes in a string in DXL, and I would suggest you allow the "-" as a separator and make sure that there is at least one number after a separator and at the start of the line number. So try like this:
 

string strToken = "^[0-9]+([\\.-][0-9]+)*$"

 


Regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Checking string using regexp
llandale - Wed May 19 15:53:17 EDT 2010

Oh. And don't forget you must be able to handle DOORS formatted paragraph 'Numbers', including "3.2.0-1" and "3.2.0-1.3" and "3.2.0-1.3.0-1".

I've never been a fan of Regular Expressions, mostly due to frustration and ignorance. Also, they are VERY inefficient in time and space. Thus, I have this very old function:

//************************
bool    fIsParaNum(string InWord)
{     // Determine if the input string appears to be a
        //   valid paragraph number.  i.e. all characters are
        //   digits, periods, or dashes, AND periods and dashes
        //   are surrounded by digits.
        // e.g. 3.12.0-4.3.0-34
 
        int     len = length(InWord)
        int     i
 
        if (len <=0) return(false)   // RETURN: empty InWord
 
        for (i=0; i<len; i++)
        { 
           if (InWord[i] != '.'         and
               InWord[i] != '-'         and
               !isdigit(InWord[i]))
              return(false)             // RETURN: illegal character
 
           if (isdigit(InWord[i])) continue     // digit OK
                                // this Char is '.' or '-'
           if (i == 0 or i == len-1)
              return(false)             // RETURN: first and last characters
                                                //   must be digits
           if (!isdigit(InWord[i-1])    or
               !isdigit(InWord[i+1]))
              return(false)             // RETURN: . and - must be surrounded by digits
                                // are not digits
        }     // end for each character in InWord.
        return(true)            // survived all checks.
}     // end fIsParaNum()

 


I suppose I should enhance it to filter for fields that have leading zeros: "3.02.1" is illegal. And perhaps a field that is exactly zero '0' must be followed by a dash. And it perhaps would be more efficient if I set "char c = InWord[i]" and used 'c' in the loop. And perhaps I should do a better job suppressing my OCD.

 

 

  • Louie

 

Re: Checking string using regexp
StefanLieser - Thu May 20 01:58:51 EDT 2010

llandale - Wed May 19 12:09:25 EDT 2010
To get one back-slash, you need to back-slash the backslash:

string strToken = "^0-9*(\\.0-9*)*$"
print strToken

prints this:
^0-9*(\.0-9*)*$

  • Louie

The double-backslash and using the "+" solved my problems.

Thank you very much!